/*****************************************************************************
 *   Copyright (C) 2008 John Schember <john@nachtimwald.com>                 *
 *                                                                           *
 *   This file is part of niwbillmanager.                                    *
 *                                                                           *
 *   niwbillmanager is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as          *
 *   published by the Free Software Foundation, either version 3 of the      *
 *   License, or (at your option) any later version.                         *
 *                                                                           *
 *   niwbillmanager is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with niwbillmanager. If not, see                                  *
 *   <http://www.gnu.org/licenses/>.                                         *
 *****************************************************************************/

#include <QCoreApplication>
#include <QDate>
#include <QFile>
#include <QFileDialog>
#include <QTextStream>

#include "icalexport.h"

#include <billitem.h>

Q_EXPORT_PLUGIN2(plugin_icalexport, iCalExport)

iCalExport::iCalExport()
{
    m_lib = 0;

    m_actionRun = new QAction("iCal Export", 0);
}

iCalExport::~iCalExport()
{
    delete m_actionRun;
}

void iCalExport::setLib(NiwBillManager *lib)
{
    m_lib = lib;
}

QAction *iCalExport::getAction()
{
    return m_actionRun;
}

QString iCalExport::getName()
{
    return "iCal Export";
}

QString iCalExport::getDescription()
{
    return "Saves unpaid bills to an ics file for importing into a calendering"
        " application.";
}

QString iCalExport::getVersion()
{
    return "1.0";
}

bool iCalExport::canConfig()
{
    return false;
}

void iCalExport::execConfigDialog()
{
}

void iCalExport::on_actionRun_triggered()
{
    QString location;

    location = QFileDialog::getSaveFileName(0,
        tr("iCal Export"), "untitled.ics",
        tr("iCal (*.ics)"));

    if (location != "") {
        exportFile(location);
    }
}

void iCalExport::exportFile(QString location)
{
    QFile data(location);

    if (data.open(QFile::WriteOnly | QFile::Truncate) && m_lib != 0) {
        QTextStream out(&data);

        // Write header
        out << "BEGIN:VCALENDAR" << endl;
        out << "PRODID:-//" << QCoreApplication::organizationName()
            << "//NONSGML " << QCoreApplication::applicationName() + " - "
            + getName() << "//EN" << endl;
        out << "VERSION:2.0" << endl;

        m_lib->startGetAllBillItems();
        while (m_lib->hasMoreBillItems()) {
            BillItem billItem;
            m_lib->getNextBillItem(billItem);

            if (!billItem.getPaid()) {
                out << "BEGIN:VEVENT" << endl;

                out << "SUMMARY:" << billItem.getName() << endl;
                out << "DESCRIPTION:" << "Amount Due: "
                    << billItem.getAmountDue()
                    << "\\n" << "Auto Pay: " << (billItem.getAutoPay()?"T":"F")
                    << "\\n" << "Payee: " << billItem.getPayee() << "\\n"
                    << "Location: " << billItem.getPaymentLocation() << "\\n"
                    << "Payment Method: " << billItem.getPaymentMethod()
                    << "\\n" << "Recurring: "
                    << billItem.getPaymentRecurringPeriod() << "\\n"
                    << "Notes: " << billItem.getNotes().replace("\n", "\\n")
                    << endl;

                out << "DTSTART;VALUE=DATE:" << billItem.getDateDue().toString(
                    "yyyyMMdd") << endl;
                out << "DTEND;VALUE=DATE:" << billItem.getDateDue().addDays(1)
                    .toString("yyyyMMdd") << endl;

                out << "END:VEVENT" << endl;
            }
        }

        // Write footer
        out << "END:VCALENDAR" << endl;

        data.close();
    }
}
